package com.lanyu.community.api.controller;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.lanyu.community.api.config.WebSocket;
import com.lanyu.community.api.util.*;
import com.lanyu.mybatis.dao.entity.CommunityUserChat;
import com.lanyu.mybatis.dao.entity.CommunityUserDiscuss;
import com.lanyu.mybatis.dao.mapperex.CommunityDynamicsMapper;
import lanyu.Say;
import org.apache.http.protocol.HttpRequestHandler;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.context.annotation.Lazy;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.data.redis.core.ZSetOperations.TypedTuple;
import org.springframework.http.ResponseEntity;
import org.springframework.ui.ModelMap;
import org.springframework.util.ClassUtils;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lanyu.community.api.beans.LocationParams;
import com.lanyu.community.api.beans.RequestPageSearchParams;
import com.lanyu.community.api.beans.ZanlistParams;
import com.lanyu.community.api.constat.CommonConstant;
import com.lanyu.community.api.entity.User;
import com.lanyu.community.api.service.ICommunityDynamicService;

import com.lanyu.community.api.shiro.JwtUtil;

import com.lanyu.mybatis.dao.entity.CommunityUserDynamics;
import com.lanyu.mybatis.dao.entity.UcenterUserinfo;
import com.lanyu.mybatis.dao.mapperex.UcenterUserinfoPlusMapper;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import redisson.lock.DistributedLocker;

/*
 * create by suibin
 * API-FEED
 * 2018-03-19
 */
@Api("API-FEED")
@RestController
@RequestMapping("feed")
public class FeedController {

	//@Autowired
	//IFeedService feedService;
	@Autowired
	UcenterUserinfoPlusMapper ucenterUserinfoPlusMapper;

	@Autowired
	CommunityDynamicsMapper communityDynamicsMapper;

	@Autowired
	ICommunityDynamicService CommunityDynamicService;

	@Resource
	private WebSocket webSocket;

	@Autowired
	@Lazy
	private RedisUtil redisUtil;
	
    @Autowired
    private DistributedLocker distributedLocker;//redis分布式锁，支付，防止金额不对。

	/*
	@ApiOperation(value = "say", notes = "")
	@RequestMapping(value = "/saya", method = RequestMethod.POST)
	@ResponseBody
	public Result saya(@RequestParam("uid") String uid) throws Exception {
		Result<String> result = new Result<String>();

		result.setResult(Say.get());

		return result;

	}
*/
	@ApiOperation(value = "动态发布", notes = "")
	@RequestMapping(value = "/publishDt", method = RequestMethod.POST)
	@ResponseBody
	public Result publishDt(@RequestBody CommunityUserDynamics cud) throws Exception {
		Result<String> result = new Result<String>();
		long timeStamp = System.currentTimeMillis();//获取当前时间戳(这是毫秒时间戳)
		cud.setdPosttime(new Date());
		result.setResult(CommunityDynamicService.publishDt(cud));
		String tojson=GsonUtil.GsonString(cud);
		//redisUtil.zAdd("dongtai:mydongtai_home_"+cud.getUid(), tojson, timeStamp);//发送到我的动态列表
		redisUtil.del("dongtai:mydongtai_home_"+cud.getUid());
		return result;
		
	}

	@Cacheable(value = "person:persondetail_cache",key = "'params_'+#uid")
	@ApiOperation(value = "个人资料接口", notes = "")
	@RequestMapping(value = "/getPersondetail", method = RequestMethod.POST)
	@ResponseBody
	public UcenterUserinfo getPersondetail(@RequestParam String uid) throws Exception {

		List<UcenterUserinfo> uulist=ucenterUserinfoPlusMapper.selectList(new QueryWrapper<UcenterUserinfo>().eq("uid",uid));


		return uulist.get(0);

	}

	@ApiOperation(value = "个人视频列表", notes = "")
	@RequestMapping(value = "/getPersonVideo", method = RequestMethod.POST)
	@ResponseBody
	public Result getPersonVideo(@RequestBody String uid,
								 @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								 @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								 HttpServletRequest req
	) throws Exception {
		Result<String> result = new Result<String>();

		return result;

	}

    @ApiOperation(value = "我的动态列表接口", notes = "")
	@RequestMapping(value = "/my_dynamics", method = RequestMethod.POST)
	@ResponseBody
	public Result<IPage<CommunityUserDynamics>> my_dynamics(
			  @RequestParam("uid") String uid,
			  @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
			  @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
			  HttpServletRequest req
			
			) throws Exception {

		Result<IPage<CommunityUserDynamics>> result = new Result<IPage<CommunityUserDynamics>>();
		
		Page<CommunityUserDynamics> page = new Page<CommunityUserDynamics>(pageNo, pageSize);
		Set<Object> xx=redisUtil.ZreverseRange("dongtai:mydongtai_home_"+uid, (long)(pageNo-1)*pageSize, (long)(pageNo*pageSize-1));
		IPage<CommunityUserDynamics> pageList =null;
    	List<CommunityUserDynamics> cud_list=new ArrayList<CommunityUserDynamics>();
    	if(xx.size()==0) {
    		//从mysql获取，
    		Map<String,Object> m = new HashMap<>();
    		m.put("uid",uid);
    		pageList =CommunityDynamicService.getCommunityDynamicsByParams(page, m);
    		if(pageList.getRecords().size()>0) {
    			Iterator<CommunityUserDynamics> iterator = pageList.getRecords().iterator();
    			while(iterator.hasNext()){
    				CommunityUserDynamics cud = iterator.next();
    				String tojson=GsonUtil.GsonString(cud);
    				redisUtil.zAdd("dongtai:mydongtai_home_"+cud.getUid(), tojson, cud.getdPosttime().getTime());//发送到我的动态列表
    			}
				redisUtil.expireDay("dongtai:mydongtai_home_"+uid,1);
    		}

    	}else {
    		//从redis获取，
			Iterator<Object> iterator = xx.iterator();
			while(iterator.hasNext()){
				Object next = iterator.next();
				CommunityUserDynamics  zz=GsonUtil.GsonToBean(next.toString(), CommunityUserDynamics.class);
				cud_list.add(zz);
				//System.out.println("value:"+next.getValue()+" score:"+next.getScore());
			}
			pageList = page.setRecords(cud_list);
    	}

		result.setSuccess(true);
		result.setResult(pageList);
		return result;    	
    	
    	
	}


	@ApiOperation(value = "聊天发布", notes = "")
	@RequestMapping(value = "/publishChat", method = RequestMethod.POST)
	@ResponseBody
	public Result publishChat(@RequestBody CommunityUserChat cud) throws Exception {

		//1,聊天信息插入MYSQL
		//2,聊天信息插入redis
		//3,相关redis缓存管理
		//4,长连接推送消息给到达用户
		Result<String> result = new Result<String>();
		cud.setCreateTime(new Date());
		String chatId=CommunityDynamicService.publishChat(cud);
		result.setResult(chatId);
		//cud.setCreateTime(null);
		//cud.setType(0);

		//cud.setMessage("");
		//查询头像
		UcenterUserinfo uu1=CommunityDynamicService.getUserinfo(cud.getFromUid());
		UcenterUserinfo uu2=CommunityDynamicService.getUserinfo(cud.getToUid());
		/*
		cud.setFromnameHead(uu1.getHead());
		cud.setTonameHead(uu2.getHead());
		cud.setFromname(uu1.getNickname());
		cud.setToname(uu2.getNickname());
		*/
		long timeStamp = System.currentTimeMillis();//获取当前时间戳(这是毫秒时间戳)
		redisUtil.zAdd("chat:mychat_person_"+cud.getFromUid(), cud.getToUid(), timeStamp);//发送到我的聊天成员列表

		String tojson1=GsonUtil.GsonString(uu1);
		String tojson2=GsonUtil.GsonString(uu2);

		String tojson3=GsonUtil.GsonString(cud);
		//更新最后一条聊天记录和时间
		redisUtil.hset("chat:mychat_person_msn_"+cud.getFromUid(), cud.getToUid(), tojson3);
		//更新聊天人的基本信息
		redisUtil.hset("chat:users_baseinfo", cud.getFromUid(), tojson1);
		redisUtil.hset("chat:users_baseinfo", cud.getToUid(), tojson2);
		redisUtil.del("chat:mychat_"+cud.getFromUid());



		JSONObject obj = new JSONObject();
		obj.put("fromnameHead", uu1.getHead());
		obj.put("createTime",new Date());
		obj.put("fromUid", cud.getFromUid());
		obj.put("message", cud.getMessage());
		obj.put("toUid",cud.getToUid() );
		obj.put("type", 0);
		obj.put("chatId", chatId);
		webSocket.sendOneMessage(cud.getToUid(), obj.toJSONString());

		return result;

	}


	@ApiOperation(value = "我的聊天人员接口", notes = "")
	@RequestMapping(value = "/my_chatPerson", method = RequestMethod.POST)
	@ResponseBody
	public Result<IPage<CommunityUserChat>> my_chatPerson(
			@RequestParam("myuid") String myuid,
			@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
			@RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
			HttpServletRequest req

	) throws Exception {


		Result<IPage<CommunityUserChat>> result = new Result<IPage<CommunityUserChat>>();

		Page<CommunityUserChat> page = new Page<CommunityUserChat>(pageNo, pageSize);
		Set<Object> xx=redisUtil.ZreverseRange("chat:mychat_person_"+myuid, (long)(pageNo-1)*pageSize, (long)(pageNo*pageSize-1));
		IPage<CommunityUserChat> pageList =null;
		List<CommunityUserChat> cud_list=new ArrayList<CommunityUserChat>();
		if(xx.size()==0) {


			//从mysql获取，
			Map<String,Object> m = new HashMap<>();
			m.put("myuid",myuid);
			pageList =CommunityDynamicService.getCommunityChatPersonList(page, m);
			if(pageList.getRecords().size()>0) {
				Iterator<CommunityUserChat> iterator = pageList.getRecords().iterator();
				while(iterator.hasNext()){
					CommunityUserChat cuc = iterator.next();
					//String tojson=GsonUtil.GsonString(cuc);

					long timeStamp = System.currentTimeMillis();//获取当前时间戳(这是毫秒时间戳)
					redisUtil.zAdd("chat:mychat_person_"+myuid, cuc.getToUid(), timeStamp);//发送到我的聊天成员列表
					UcenterUserinfo cuc1=new UcenterUserinfo();
					cuc1.setNickname(cuc.getToname());
					cuc1.setHead(cuc.getTonameHead());
					String tojson1=GsonUtil.GsonString(cuc1);
					redisUtil.hset("chat:users_baseinfo", cuc.getToUid(), tojson1);
					String tojson3=GsonUtil.GsonString(cuc);
					redisUtil.hset("chat:mychat_person_msn_"+myuid,cuc.getToUid(), tojson3);
					cud_list.add(cuc);
				}

			}


			pageList = page.setRecords(cud_list);
		}else {
			//从redis获取，
			Iterator<Object> iterator = xx.iterator();
			while(iterator.hasNext()){
				Object next = iterator.next();
				String beanstring=(String) redisUtil.hget("chat:users_baseinfo", next.toString());

				String message=(String) redisUtil.hget("chat:mychat_person_msn_"+myuid, next.toString());

				CommunityUserChat get_message=GsonUtil.GsonToBean(message, CommunityUserChat.class);

				UcenterUserinfo to_uui=GsonUtil.GsonToBean(beanstring, UcenterUserinfo.class);

				//CommunityUserChat  zz=new CommunityUserChat();
				get_message.setFromUid(myuid);
				get_message.setToUid(next.toString());
				get_message.setTonameHead(to_uui.getHead());
				get_message.setToname(to_uui.getNickname());

				cud_list.add(get_message);
				//System.out.println("value:"+next.getValue()+" score:"+next.getScore());
			}
			pageList = page.setRecords(cud_list);
		}

		result.setSuccess(true);
		result.setResult(pageList);
		return result;

	}


	@ApiOperation(value = "我的聊天记录接口2", notes = "")
	@RequestMapping(value = "/my_chat2", method = RequestMethod.POST)
	@ResponseBody
	public Result<IPage<CommunityUserChat>> my_chat2(
			@RequestParam("myuid") String myuid,
			@RequestParam("touid") String touid,
			@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
			@RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
			HttpServletRequest req

	) throws Exception {
/*
		Result<IPage<CommunityUserChat>> result = new Result<IPage<CommunityUserChat>>();

		Page<CommunityUserChat> page = new Page<CommunityUserChat>(pageNo, pageSize);
		IPage<CommunityUserChat> pageList = null;
		//从mysql获取，
		Map<String, Object> m = new HashMap<>();
		m.put("myuid", myuid);
		m.put("touid", touid);
		pageList = CommunityDynamicService.getCommunityChatList(page, m);
		result.setSuccess(true);
		result.setResult(pageList);
		return result;
*/

		Result<IPage<CommunityUserChat>> result = new Result<IPage<CommunityUserChat>>();

		Page<CommunityUserChat> page = new Page<CommunityUserChat>(pageNo, pageSize);
		Set<Object> xx=redisUtil.ZreverseRange("chat:mychat_"+myuid, (long)(pageNo-1)*pageSize, (long)(pageNo*pageSize-1));
		IPage<CommunityUserChat> pageList =null;
		List<CommunityUserChat> cud_list=new ArrayList<CommunityUserChat>();
		if(xx.size()==0) {
			//从mysql获取，
			Map<String,Object> m = new HashMap<>();
			m.put("myuid", myuid);
			m.put("touid", touid);
			pageList =CommunityDynamicService.getCommunityChatList(page, m);
			if(pageList.getRecords().size()>0) {
				Iterator<CommunityUserChat> iterator = pageList.getRecords().iterator();
				while(iterator.hasNext()){
					CommunityUserChat cud = iterator.next();
					String tojson=GsonUtil.GsonString(cud);
					redisUtil.zAdd("chat:mychat_"+cud.getFromUid(), tojson, cud.getCreateTime().getTime());//发送到我的动态列表
				}
				redisUtil.expireDay("chat:mychat_"+myuid,1);
			}

		}else {
			//从redis获取，
			Iterator<Object> iterator = xx.iterator();
			while(iterator.hasNext()){
				Object next = iterator.next();
				CommunityUserChat  zz=GsonUtil.GsonToBean(next.toString(), CommunityUserChat.class);
				cud_list.add(zz);
				//System.out.println("value:"+next.getValue()+" score:"+next.getScore());
			}
			pageList = page.setRecords(cud_list);
		}

		result.setSuccess(true);
		result.setResult(pageList);
		return result;

	}



	@ApiOperation(value = "我的聊天记录接口", notes = "")
	@RequestMapping(value = "/my_chat", method = RequestMethod.POST)
	@ResponseBody
	public Result<IPage<CommunityUserChat>> my_chat(
			@RequestParam("myuid") String myuid,
			@RequestParam("touid") String touid,
			@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
			@RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
			HttpServletRequest req

	) throws Exception {

		Result<IPage<CommunityUserChat>> result = new Result<IPage<CommunityUserChat>>();

		Page<CommunityUserChat> page = new Page<CommunityUserChat>(pageNo, pageSize);
		IPage<CommunityUserChat> pageList = null;
		//从mysql获取，
		Map<String, Object> m = new HashMap<>();
		m.put("myuid", myuid);
		m.put("touid", touid);
		pageList = CommunityDynamicService.getCommunityChatList(page, m);
		result.setSuccess(true);
		result.setResult(pageList);
		return result;


	}

	@ApiOperation(value = "关注用户的动态列表接口", notes = "")
	@RequestMapping(value = "/guanzhu_dynamics", method = RequestMethod.POST)
	@ResponseBody
	public Result<IPage<CommunityUserDynamics>> guanzhu_dynamics(
			@RequestParam("uid") String uid,
			@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
			@RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
			HttpServletRequest req

	) throws Exception {

		Result<IPage<CommunityUserDynamics>> result = new Result<IPage<CommunityUserDynamics>>();

		Page<CommunityUserDynamics> page = new Page<CommunityUserDynamics>(pageNo, pageSize);
		IPage<CommunityUserDynamics> pageList =null;
		//从mysql获取，
		Map<String,Object> m = new HashMap<>();
		m.put("from_uid",uid);
		pageList =CommunityDynamicService.getCommunityGuanzhuDynamicsLatestweek(page, m);
		result.setSuccess(true);
		result.setResult(pageList);
		return result;


	}


	@ApiOperation(value = "动态删除", notes = "")
	@RequestMapping(value = "/delDt", method = RequestMethod.POST)
	@ResponseBody
	public Result<String> delDt(
			@RequestParam("uid") String uid,
			@RequestParam("did") String did
			) throws Exception {
		int x=CommunityDynamicService.delDt(uid, did);
		Result<String> result = new Result<String>();
        result.setResult(x+"");
		redisUtil.del("dongtai:mydongtai_home_"+uid);
        return result;
	}

    @ApiOperation(value = "关注", notes = "")
	@ApiImplicitParams({ 
		@ApiImplicitParam(paramType = "query", dataType = "string", name = "from_uid", value = "来自用户ID", required = true), 
		@ApiImplicitParam(paramType = "query", dataType = "string", name = "to_uid", value = "要关注的用户ID", required = true),
	})
	@RequestMapping(value = "/follow", method = RequestMethod.POST)
	@ResponseBody
	public Result<String> follow(
			@RequestParam("from_uid") String from_uid,
			@RequestParam("to_uid") String to_uid
			) throws Exception {
    	int x=CommunityDynamicService.follow(from_uid, to_uid);    	
		Result<String> result = new Result<String>();
        result.setResult(x+"");
        return result;
	}
    
    @ApiOperation(value = "取消关注", notes = "")
	@ApiImplicitParams({ 
		@ApiImplicitParam(paramType = "query", dataType = "string", name = "from_uid", value = "来自用户ID", required = true), 
		@ApiImplicitParam(paramType = "query", dataType = "string", name = "to_uid", value = "要关注的用户ID", required = true),
	})
	@RequestMapping(value = "/unfollow", method = RequestMethod.POST)
	@ResponseBody
	public Result<String> unfollow(
			@RequestParam("from_uid") String from_uid,
			@RequestParam("to_uid") String to_uid
			) throws Exception {
    	int x=CommunityDynamicService.unfollow(from_uid, to_uid);    	
		Result<String> result = new Result<String>();
        result.setResult(x+"");
        return result;
	}
    
    
    @ApiOperation(value = "获取当前登录用户关注用户列表接口", notes = "")
	@RequestMapping(value = "/get_followlist", method = RequestMethod.POST)
	@ResponseBody
	public Result<IPage<ZanlistParams>> get_followlist(
		@RequestParam("from_uid") String from_uid,
		@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
		@RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
		  HttpServletRequest req
			) throws Exception {
    	
		Result<IPage<ZanlistParams>> result = new Result<IPage<ZanlistParams>>();
		
		Page<ZanlistParams> page = new Page<ZanlistParams>(pageNo, pageSize);
		//获取zset里的uid
		Set<Object> xx=redisUtil.ZreverseRange("follow:follow_guanzhu_"+from_uid, (long)(pageNo-1)*pageSize, (long)(pageNo*pageSize-1));
		IPage<ZanlistParams> pageList =null;
    	List<ZanlistParams> cud_list=new ArrayList<ZanlistParams>();
    	if(xx.size()>0) {
    
    		//遍历关注的用户uid
			Iterator<Object> iterator = xx.iterator();
			while(iterator.hasNext()){
				Object next = iterator.next();
				String beanstring=(String) redisUtil.hget("user:users_baseinfo", next.toString());
				ZanlistParams zp=new ZanlistParams();
				try {
					UcenterUserinfo  zz=GsonUtil.GsonToBean(beanstring, UcenterUserinfo.class);
					zp.setCreatetime(zz.getCreateTime());
					zp.setHead(zz.getHead());
					zp.setNickname(zz.getNickname());
					zp.setUsername(zz.getUsername());
					cud_list.add(zp);
				}catch(Exception e) {
					
				}
				
				//System.out.println("value:"+next.getValue()+" score:"+next.getScore());
			}
			pageList = page.setRecords(cud_list);
    	}

		result.setSuccess(true);
		result.setResult(pageList);
		return result;
	}  
    
    @ApiOperation(value = "获取当前登录用户粉丝用户列表接口", notes = "")
	@RequestMapping(value = "/get_followedlist", method = RequestMethod.POST)
	@ResponseBody
	public  Result<IPage<UcenterUserinfo>> get_followedlist(
			  @RequestParam("from_uid") String from_uid,
			  @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
			  @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
			  HttpServletRequest req			
			) throws Exception {
		Result<IPage<UcenterUserinfo>> result = new Result<IPage<UcenterUserinfo>>();
		
		Page<UcenterUserinfo> page = new Page<UcenterUserinfo>(pageNo, pageSize);
	
		Map<String,Object> m = new ConcurrentHashMap<>();
		m.put("uid",from_uid);
		IPage<UcenterUserinfo> pageList = CommunityDynamicService.getFensiUserinfos(page, m);
		result.setSuccess(true);
		result.setResult(pageList);
		return result;
	}    

    
    @ApiOperation(value = "首页推荐动态", notes = "")
	@RequestMapping(value = "/recommend_dynamic", method = RequestMethod.POST)
	@ResponseBody
	public Result<IPage<CommunityUserDynamics>> recommend_dynamic(
			@RequestBody LocationParams cp,
			  @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
			  @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
			  HttpServletRequest req
			) throws Exception {
    	//1、根据定位，查找用户
    	//BigDecimal lat=cp.getLat();//纬度
    	//BigDecimal lng=cp.getLng();//经度
    	//String location=cp.getLocation();
    	//List<UcenterUserinfo> uulist=ucenterUserinfoPlusMapper.selectList(new QueryWrapper<UcenterUserinfo>().eq("location",location));
    	//2、查询用户的动态
    	//3、按时间倒序

		Result<IPage<CommunityUserDynamics>> result = new Result<IPage<CommunityUserDynamics>>();
		
		Page<CommunityUserDynamics> page = new Page<CommunityUserDynamics>(pageNo, pageSize);
	
		Map<String,Object> m = new HashMap<>();
		m.put("location",cp.getLocation());
		IPage<CommunityUserDynamics> pageList = CommunityDynamicService.getCommunityDynamicsLatestweek(page, m);
		result.setSuccess(true);
		result.setResult(pageList);
		return result;
	}  

	@ApiOperation(value = "点赞", notes = "")
	@RequestMapping(value = "/zan", method = RequestMethod.POST)
	@ResponseBody
	public Result<String> zan(@RequestParam("uid") String uid,@RequestParam("did") String did) throws Exception {
	
		Result<String> result = new Result<String>();
        //1.从redis中取出用户吐槽点赞记录,24小时内防止重复点赞
        String flag = (String) redisUtil.get("dianzan:userzan_" + did + "_" + uid);
        //2.如果没有记录，让用户点赞，并且把点赞记录存入redis中
        if (flag == null) {
			redisUtil.set("dianzan:userzan_" + did + "_" + uid, "1", 1*3600*24);
        	int ok=CommunityDynamicService.upzan(uid, did);
        	if(ok>0) {
        		//一天内不能重复点赞

        		result.setResult("1");
        		result.success("吐槽点赞成功!");
        		//清除did该动态的用户动态列表缓存，用于更新点赞数。
				Wrapper<CommunityUserDynamics> wrapper=new QueryWrapper<CommunityUserDynamics>()
						.eq("did", did)
						;
				CommunityUserDynamics cud=communityDynamicsMapper.selectOne(wrapper);
				redisUtil.del("dongtai:mydongtai_home_"+cud.getUid());
        	}else {
        		result.error500("数据库点赞异常");
        	}
            
        } else {
            //3.如果有记录，提示“不能重复点赞”
        	result.error500("不能重复点赞");
        }
        return result;

	}

	@ApiOperation(value = "获取动态的评论列表", notes = "")
	@RequestMapping(value = "/dt_discusslist", method = RequestMethod.POST)
	@ResponseBody
	public Result<IPage<CommunityUserDiscuss>>  dt_discusslist(
			@RequestParam("did") String did,
			@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
			@RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
			HttpServletRequest req
	) throws Exception {

		Result<IPage<CommunityUserDiscuss>> result = new Result<IPage<CommunityUserDiscuss>>();

		Page<CommunityUserDiscuss> page = new Page<CommunityUserDiscuss>(pageNo, pageSize);
		Set<Object> xx=redisUtil.ZreverseRange("discuss:discusslist_"+did, (long)(pageNo-1)*pageSize, (long)(pageNo*pageSize-1));
		IPage<CommunityUserDiscuss> pageList =null;
		List<CommunityUserDiscuss> cud_list=new ArrayList<CommunityUserDiscuss>();
		if(xx.size()==0) {
			//从mysql获取，
			Map<String, Object> m = new HashMap<>();
			m.put("did", did);
			pageList = CommunityDynamicService.getDiscussList(page, m);

			if(pageList.getRecords().size()>0) {
				Iterator<CommunityUserDiscuss> iterator = pageList.getRecords().iterator();
				while(iterator.hasNext()){
					CommunityUserDiscuss cud = iterator.next();
					String tojson=GsonUtil.GsonString(cud);
					redisUtil.zAdd("discuss:discusslist_"+cud.getDiscDid(), tojson, cud.getDiscCreatime().getTime());//发送到我的动态列表

				}
				redisUtil.expireDay("discuss:discusslist_"+did,1);
			}


		}else {
			//从redis获取，
			Iterator<Object> iterator = xx.iterator();
			while(iterator.hasNext()){
				Object next = iterator.next();
				CommunityUserDiscuss  zz=GsonUtil.GsonToBean(next.toString(), CommunityUserDiscuss.class);
				cud_list.add(zz);
				//System.out.println("value:"+next.getValue()+" score:"+next.getScore());
			}
			pageList = page.setRecords(cud_list);
		}
		result.setSuccess(true);
		result.setResult(pageList);
		return result;
	}
	
	@ApiOperation(value = "发表评论", notes = "")
	@RequestMapping(value = "/add_discuss", method = RequestMethod.POST)
	@ResponseBody
	public Result<String> add_discuss(@RequestBody CommunityUserDiscuss cud) throws Exception {
		long timeStamp = System.currentTimeMillis();//获取当前时间戳(这是毫秒时间戳)
		cud.setDiscCreatime(new Date());
		String x= CommunityDynamicService.addDiscuss(cud);
		Result<String> result = new Result<String>();
		result.setResult(x+"");
		String tojson=GsonUtil.GsonString(cud);
		redisUtil.del("discuss:discusslist_"+cud.getDiscDid());//评论后，清一下缓存。查询的时候数据库加载一份到缓存。
		//通过did查询所属uid，删除该用户的redis我的动态缓存，
		Wrapper<CommunityUserDynamics> wrapper=new QueryWrapper<CommunityUserDynamics>()
				.eq("did", cud.getDiscDid())
				;
		CommunityUserDynamics cud1=communityDynamicsMapper.selectOne(wrapper);
		redisUtil.del("dongtai:mydongtai_home_"+cud1.getUid());


		//redisUtil.zAdd("discuss:discusslist_"+cud.getDiscDid(), tojson, timeStamp);//发送到我的动态列表
		return result;


	}

	@ApiOperation(value = "删除评论", notes = "")
	@RequestMapping(value = "/del_discuss", method = RequestMethod.POST)
	@ResponseBody
	public Result<String> del_discuss(
			@RequestParam("did") String did,
			@RequestParam("discId") String discId,
			@RequestParam("uid") String uid

	) throws Exception {
		int x= CommunityDynamicService.delDiscuss(discId,uid);

		Result<String> result = new Result<String>();
		result.setResult(x+"");
		redisUtil.del("discuss:discusslist_"+did);//
		return result;
	}

    @ApiOperation(value = "撤回聊天记录", notes = "")
    @RequestMapping(value = "/delChat", method = RequestMethod.POST)
    @ResponseBody
    public Result<String> delChat(
			@RequestParam("touser") String touser,
            @RequestParam("chat_id") String chat_id,
			@RequestParam("index") String index
    ) throws Exception {
        int x= CommunityDynamicService.delChat(chat_id);

		JSONObject obj = new JSONObject();
		obj.put("fromnameHead", "");
		obj.put("createTime",new Date());
		obj.put("fromUid", "");
		obj.put("message", "");
		obj.put("toUid", "");
		obj.put("type", 4);
		obj.put("chatId", chat_id);
		obj.put("index", Integer.parseInt(index));
		webSocket.sendOneMessage(touser, obj.toJSONString());

        Result<String> result = new Result<String>();
        result.setResult(x+"");
        return result;
    }


	@ApiOperation(value = "点赞人列表（谁给点的赞）", notes = "")
	@RequestMapping(value = "/zan_userlist", method = RequestMethod.POST)
	@ResponseBody
	public Result<IPage<ZanlistParams>> zan_userlist(@RequestParam("did") String did,
			  @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
			  @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
			  HttpServletRequest req
			) throws Exception {
	
		Result<IPage<ZanlistParams>> result = new Result<IPage<ZanlistParams>>();
		
		Page<ZanlistParams> page = new Page<ZanlistParams>(pageNo, pageSize);
		IPage<ZanlistParams> pageList = CommunityDynamicService.getZanlist(page,did);
		result.setSuccess(true);
		result.setResult(pageList);
		return result;
	}
	/*
	@ApiOperation(value = "评论人列表（谁给评论）", notes = "")
	@RequestMapping(value = "/comment_userlist", method = RequestMethod.GET)
	@ResponseBody
	public String comment_userlist() throws Exception {
		//feedService.register();
		return "------111--------";
	}

	 */
}
